package com.open.free.generator.project;

import com.open.free.generator.utils.FileIOUtils;
import com.open.free.generator.utils.FreemarkerUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

/**
 * @author Faye.Wang
 * @version 1.0
 * @date 2020/12/19 12:32
 * @Description
 */
@Component
public class ProjectSrcGenerator {

    public static final String SRC_MAIN_PACKAGE=File.separator+"src"+ File.separator+"main"+File.separator+"java";
    public static final String SRC_TEST_PACKAGE=File.separator+"src"+ File.separator+"test"+File.separator+"java";
    public static final String SRC_MAIN_RESOURCES_PACKAGE=File.separator+"src"+ File.separator+"main"+File.separator+"resources";
    /**
     * 生成Src目录下的包
     * @param targetPath
     * @param packagePath
     * @param module
     * @return 路径为 targetPath/src/man/java/com/xx/xxx/module
     * @throws IOException
     */
    public String createSrcPackage(String targetPath,String packagePath,String module) throws IOException {
        String packPath = getPackagePath(packagePath);
        String projectPath = targetPath+SRC_MAIN_PACKAGE+File.separator+packPath+module;
        FileIOUtils.getFileExists(projectPath);
        FileIOUtils.getFileExists(targetPath+SRC_TEST_PACKAGE+File.separator+packPath);
        return projectPath;
    }


    private String getPackagePath(String packagePath){
        String[] packageArrays = StringUtils.split(packagePath,".");
        StringBuilder stringBuilder = new StringBuilder();
        for(String str: packageArrays){
            stringBuilder.append(str).append(File.separator);
        }
        return stringBuilder.toString();
    }

    public String createSrcResourcesPackage(String targetPath) throws IOException {
        String projectPath = targetPath+SRC_MAIN_RESOURCES_PACKAGE;
        FileIOUtils.getFileExists(projectPath+File.separator+"static");
        FileIOUtils.getFileExists(projectPath+File.separator+"templates");
        return projectPath;
    }

}
